package com.yhsoft.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhsoft.common.response.PageResult;
import com.yhsoft.common.vo.Result;
import com.yhsoft.goods.controller.Po.SetSkuInfoPo;
import com.yhsoft.goods.controller.Po.skuInfoPo;
import com.yhsoft.goods.controller.requset.DelDisByGoodsIdInVo;
import com.yhsoft.goods.controller.requset.SetMallAddPriceInVo;
import com.yhsoft.goods.controller.requset.SetMallUpStatusInVo;
import com.yhsoft.goods.controller.requset.SetSkuAdvocatePriceInVo;
import com.yhsoft.goods.controller.response.GetMallAddPriceOutVo;
import com.yhsoft.goods.entity.CompanyGoods;
import com.yhsoft.goods.entity.SkuInfo;
import com.yhsoft.goods.service.ICompanyGoodsService;
import com.yhsoft.goods.service.ISkuInfoService;
import com.yhsoft.sys.controller.Response.DistributorListOutVo;
import com.yhsoft.sys.entity.CompanyInfo;
import com.yhsoft.sys.service.ICompanyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 渠道商商品关联表 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-22
 */
@Api(tags = {"C-渠道商与商品关联接口列表"})
@RestController
@RequestMapping("/companyGoods")
public class CompanyGoodsController {

    @Resource
    private ICompanyInfoService companyInfoService;

    @Resource
    private ICompanyGoodsService iCompanyGoodsService;

    @Resource
    private ISkuInfoService iSkuInfoService;

    @ApiOperation("根据商品分页查询适用渠道商")
    @GetMapping("/getDistListByGoodId")
    public Result<PageResult<DistributorListOutVo>> getDistListByGoodId(@RequestParam(value = "goodId") @ApiParam(value = "主键") Integer id,
                                                                        @RequestParam(value = "distributorName", required = false) @ApiParam(value = "渠道商名称") String companyName,
                                                                        @RequestParam(value = "contactsPhone", required = false) @ApiParam(value = "联系人电话") String contactsPhone,
                                                                        @RequestParam(value = "contacts", required = false) @ApiParam(value = "联系人") String contacts,
                                                                        @RequestParam(value = "pageNo") Long pageNo,
                                                                        @RequestParam(value = "pageSize") Long pageSize) {
        LambdaQueryWrapper<CompanyInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(companyName), CompanyInfo::getCompanyName, companyName);
        wrapper.like(StringUtils.hasLength(contactsPhone), CompanyInfo::getContactsPhone, contactsPhone);
        wrapper.like(StringUtils.hasLength(contacts), CompanyInfo::getContacts, contacts);
        wrapper.inSql(CompanyInfo::getId, "select company_id as id from g_company_goods where deleted = 0 and goods_id = " + id);
        wrapper.orderByDesc(CompanyInfo::getId);

        Page<CompanyInfo> page = new Page<>(pageNo, pageSize);
        companyInfoService.page(page, wrapper);
        return Result.success(PageResult.valueOf(page, DistributorListOutVo::valueOf));
    }

    @ApiOperation("删除适用渠道商")
    @PostMapping("/delDisByGoodsId")
    public Result<?> delDisByGoodsId(@RequestBody DelDisByGoodsIdInVo ids) {
        if (ids.getGoodsid() == null || ids.getDistributorIds().size() == 0) {
            return Result.success("输入非法！");
        }
        LambdaQueryWrapper<CompanyGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyGoods::getGoodsId, ids.getGoodsid());
        wrapper.in(CompanyGoods::getCompanyId, ids.getDistributorIds());
        iCompanyGoodsService.getBaseMapper().delete(wrapper);
        return Result.success("删除成功！");
    }

    @ApiOperation("分页查询未选中适用渠道商")
    @GetMapping("/getNotSelectedDistListByGoodId")
    public Result<PageResult<DistributorListOutVo>> getNotSelectedDistListByGoodId(@RequestParam(value = "goodId") Integer id,
                                                                                   @RequestParam(value = "distributorName", required = false) @ApiParam(value = "渠道商名称") String companyName,
                                                                                   @RequestParam(value = "phone", required = false) @ApiParam(value = "手机号") String contactsPhone,
                                                                                   @RequestParam(value = "contacts", required = false) @ApiParam(value = "联系人") String contacts,
                                                                                   @RequestParam(value = "pageNo") Long pageNo,
                                                                                   @RequestParam(value = "pageSize") Long pageSize) {
        LambdaQueryWrapper<CompanyInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(companyName), CompanyInfo::getCompanyName, companyName);
        wrapper.eq(StringUtils.hasLength(contactsPhone), CompanyInfo::getContactsPhone, contactsPhone);
        wrapper.like(StringUtils.hasLength(contacts), CompanyInfo::getContacts, contacts);
        wrapper.notInSql(CompanyInfo::getId, "select company_id as id from g_company_goods where deleted = 0 and goods_id = " + id);
        wrapper.orderByDesc(CompanyInfo::getId);

        Page<CompanyInfo> page = new Page<>(pageNo, pageSize);
        companyInfoService.page(page, wrapper);
        return Result.success(PageResult.valueOf(page, DistributorListOutVo::valueOf));
    }

    @ApiOperation("新增适用渠道商")
    @PostMapping("/selectDisByGoodsId")
    public Result<?> selectDisByGoodsId(@RequestBody DelDisByGoodsIdInVo ids) {
        if (ids.getGoodsid() == null || ids.getDistributorIds().size() == 0) {
            return Result.success("输入非法！");
        }
        Integer goodsId = ids.getGoodsid();

        // 删除历史数据
        LambdaQueryWrapper<CompanyGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyGoods::getGoodsId, goodsId);
        iCompanyGoodsService.remove(wrapper);

        for (Integer a : ids.getDistributorIds()) {
            CompanyGoods goods = new CompanyGoods();
            goods.setGoodsId(goodsId);
            goods.setCompanyId(a);

            // 查询默认的服务费
            CompanyInfo companyInfo = companyInfoService.getById(a);
            if(companyInfo != null && StringUtils.hasLength(companyInfo.getDefaultCharge())) {
                goods.setCustomCharge(companyInfo.getDefaultCharge());
            }

            goods.setDeleted(0);
            iCompanyGoodsService.getBaseMapper().insert(goods);
        }
        return Result.success("新增成功！");
    }

    @ApiOperation("查询渠道商商品加价")
    @GetMapping("/getMallAddPrice")
    public Result<List<GetMallAddPriceOutVo>> getMallAddPrice(@RequestParam(value = "goodsId") Integer goodsId) {
        List<GetMallAddPriceOutVo> skuInfos = iSkuInfoService.getMallAddPrice(goodsId);
        return Result.success(skuInfos);
    }

    @ApiOperation("设置渠道商商品加价")
    @PostMapping("/setMallAddPrice")
    public Result<?> setMallAddPrice(@Validated @RequestBody List<SetMallAddPriceInVo> inVos) {
        String result =iSkuInfoService.setMallAddPrice(inVos);
        if(result.equals("success")){
            return Result.success(result);
        }
        return Result.fail("设置渠道商商品加价失败！");
    }

    @ApiOperation("渠道商设置商品上架状态")
    @PostMapping("/setMallUpStatus")
    public Result<?> setMallUpStatus(@Validated @RequestBody SetMallUpStatusInVo inVo) {
        String result = iCompanyGoodsService.setMallUpStatus(inVo);
        if(result.equals("success")){
            return Result.success(result);
        }
        return Result.fail("设置渠道商商品上架状态失败！");
    }
}
